package com.cxx.home.service.impl;

import com.cxx.common.dto.StatsChartDto;
import com.cxx.framework.web.CustomException;
import com.cxx.home.dao.AnniversaryDao;
import com.cxx.home.dto.anniversary.AnniversaryDto;
import com.cxx.home.entity.Anniversary;
import com.cxx.home.mapper.AnniversaryMapper;
import com.cxx.home.service.AnniversaryService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnniversaryServiceImpl implements AnniversaryService {
    @Resource
    private AnniversaryDao anniversaryDao;

    @Resource
    private AnniversaryMapper anniversaryMapper;


    @Override
    public Boolean addAnniversary(AnniversaryDto anniversaryDto) {
        Anniversary anniversary = new Anniversary();
        BeanUtils.copyProperties(anniversaryDto, anniversary);
        anniversary.setRecurring(anniversaryDto.getRecurring() ? 1 : 0);

        return anniversaryMapper.insert(anniversary) == 1;
    }

    @Override
    public Boolean updateAnniversary(Long id, AnniversaryDto anniversaryDto) {
        if (anniversaryMapper.selectById(id) == null) {
            throw new CustomException("该纪念日不存在");
        }

        Anniversary anniversary = new Anniversary();
        BeanUtils.copyProperties(anniversaryDto, anniversary);
        anniversary.setId(id);
        anniversary.setRecurring(anniversaryDto.getRecurring() ? 1 : 0);
        return anniversaryMapper.updateById(anniversary) == 1;
    }

    @Override
    public Boolean deleteAnniversary(Long id) {
        if (anniversaryMapper.selectById(id) == null) {
            throw new CustomException("该纪念日不存在");
        }

        return anniversaryMapper.deleteById(id) == 1;
    }

    @Override
    public List<AnniversaryDto> queryAnniversary(String category) {
        return anniversaryDao.queryAnniversary(category);
    }

    @Override
    public List<StatsChartDto> queryAnniversaryCategory() {
        return anniversaryDao.queryAnniversaryCategory();
    }
}
